<?php
/**
 * Class User_verify_service 用户验证相关业务逻辑
 * @author: Calvin
 * @email : hongbinfu@163.com
 * @readme: 用户验证相关业务逻辑处理类
 */

class User_verify_service extends MY_Service
{

    const PROCESS_REGISTER=1;
    const PROCESS_FIND_PASSWORD=2;
    const PROCESS_MODIFY_PHONE=3;
    const PROCESS_MODIFY_EMAIL=4;
    const PROCESS_VALID_PHONE=5;
    const PROCESS_VALID_EMAIL=6;
    const PROCESS_VALID_OLD_PHONE = 7;
    const PROCESS_BIND_AUTH_USER = 8;

    const WAY_EMAIL_LINK=1; //邮箱链接方式
    const WAY_SMS=2;//短信验证码方式
    const WAY_EMAIL=3;//邮件验证码方式
    public function __construct()
    {
        parent::__construct();
        $this->load->model('user_model');
        $this->config->load("user");
        $this->load->language(array('user','sender'));
        $this->load->library("sms");
        //加载CI的email类
        $this->load->library('email');
    }
    //获取系统默认的邮箱发送账号
    public function get_sender_email(){
        return $this->config->item("verify_email_from_account","user_common");
    }

    public function check_email_not_active($email)
    {
        if(empty($email))
        {
            return $this->set_error(1003);
        }
        $user_info = $this->user_model->get_user_info_by_email($email,"id,verify,status");
        if(empty($user_info))
        {
            return $this->set_error(5004);//用户不存在
        }
        else if($user_info['verify']==1)
        {
            return $this->set_error(5007);//该账号已经激活
        }
        else
        {
            return TRUE;
        }
    }

    /**
     * 检查是否可以发送注册激活邮件
     * @param $email
     * @return bool
     */
    public function can_send_register_auth_email($email)
    {
        //检查email是否有关联的用户
        $user_info = $this->user_model->get_user_info_by_email($email,"id,verify");
        if(empty($user_info))
        {
            return $this->set_error(5004);//用户不存在
        }
        else if($user_info['verify']==1)
        {
            return $this->set_error(5007);//该账号已经激活
        }
        $result = $this->get_verify_info_by_email(1,$email,"id,num,time");
        if(!empty($result)){
            //防止不断刷新发送邮件
            $time = time() - (int)$result['time'];
            $times = $this->config->item("register_email_times","register");
            //$exp = $this->config->item("email_verify_code_exp","user_common");
            $interval_time = $this->config->item("register_email_interval_time","register");
            //1分钟内只能发送一次
            if($interval_time!=0 && $time < $interval_time)
            {
                return $this->set_error(6003,array(Common::format_second($interval_time)));
            }
            //24小时内只能发送5次
            if($times !=0 && (int)$result['num'] >= $times && $time <= 86400)
            {
                return $this->set_error(6004,array($times));
            }
            //超过24小时的记录删除
            if($time>86400)
            {
                $this->user_model->delete_verify_info(array("id"=>$result['id']));
                return TRUE;
            }
        }
        return TRUE;
    }

    /**
     * 发送注册检查邮件
     */
    public function send_register_auth_email($email)
    {
        //检查email是否有关联的用户
        $user_info = $this->user_model->get_user_info_by_email($email,"id,verify");
        if(empty($user_info))
        {
            return $this->set_error(5004);//用户不存在
        }
        else if($user_info['verify']==1)
        {
            return $this->set_error(5007);//该账号已经激活
        }
        //生成唯一标识符
        $uniqid = Common::uniqid();
        //加载CI的email类
       // $this->load->library('email');
        //以下设置Email内容
        $host = site_url();
        $url =  site_url('register/active/'.$uniqid);
        //发送者
        $from_account = $this->get_sender_email();
        $html = <<<EOT
		<style type="text/css">
		*{margin:0; padding:0;}
		</style>
		<div style="width:600px; margin:0 auto; font:14px/1.5 Microsoft Yahei UI,Microsoft Yahei,tahoma,arial,宋体; background-color:#fbfbfb;">
		<div><a href="$host" target="_blank"><img src="$host/static/img/email/logo.png" alt="易控"></a></div>
		<div style="color:#5f5f5f; font-size:13px; margin:32px 37px 65px 37px; line-height:25px;">
			<p style="margin-bottom:8px;">尊敬的用户：</p>
			<p>欢迎注册易控！</p>
			<p>请点击下面的按钮完成注册邮箱激活，验证完成之后可通过此邮箱找回密码：</p>
			<p style="text-align:center; margin:35px 0;"><a href="$url" target="_blank" style="color: #ffffff; display: inline-block; font-size: 18px; height: 43px; line-height: 38px; width: 146px; background-color: #e53e46; border: 1px solid #e43e46; border-radius: 1px;  box-shadow: 0 -4px #ca181c inset; cursor: pointer; text-align: center; text-decoration:none;">激活账户</a></p>
			<p>如果以上按钮点击无效，请将下面的链接地址复制到您的浏览器地址栏（如IE）</p>
			<p>进入到易控邮箱激活页面。<span style="color:#817d7d;">（该链接在24小时有效，超过24小时需要重新注册）</span></p>
			<p><a href="$url" target="_blank" style="color:#73aeff; word-break: break-all; text-decoration:none;">$url</a></p>
		</div>
		<div style="font-size:12px; color:#8e8e8e; margin:30px 37px 0 37px; text-align:center;">©2016 易控<img src="$host/static/img/email/logo-info.png" alt="我的信息我做主" style="vertical-align: middle; margin-left: 12px;"></div>
	</div>
EOT;
        $this->email->from($from_account[0], $from_account[1]);
        $this->email->to($email);
        $this->email->subject('易控-激活您的账号！');
        $this->email->message($html);
        //$this->email->send();

        if(!$this->email->send()){
            return $this->set_error(6001);
        }
        else
        {
            //发送成功后更新数据表
            $data = array(
                'process'   => 1,//注册流程
                'uid'       => $user_info['id'],//用户id
                'way'       => 1,//认证方式 邮件
                'receiver'  => $email,//接受者
                'code'    => $uniqid,//校验码
                'code_hash' => Common::time33($uniqid),//校验码的哈希值
                'time'      => time()
            );
            //查找是否已经存在记录
            $map=array(
                'process'=>1,
                'way'=>1,
                'uid'=>$user_info['id'],
                'receiver'=>$email
            );
            $find = $this->user_model->get_verify_info($map,"id,num");
            if(!empty($find)){
                //更新
                $num = (int)$find['num'];
                $data['num'] = ++$num;
                $result = $this->user_model->update_verify_info_by_id($find['id'], $data);
            } else {
                //插入新记录
                $result = $this->user_model->create_verify_info($data);
            }
            if($result===FALSE)
            {
                return $this->set_error(1002);
            }
            else
            {
                return TRUE;
            }
        }
    }




    /**
     *
     * 发送找回密码邮箱验证
     * @param $email
     * @return bool
     */
    public function send_find_password_email($email)
    {
        //检查email是否有关联的用户
        $user_info = $this->user_model->get_user_info_by_email($email);
        if(empty($user_info))
        {
            return $this->set_error(5004);//用户不存在
        }
        //生成唯一标识符
        $uniqid = Common::uniqid();
        //加载CI的email类
        //$this->load->library('email');
        //以下设置Email内容$host = 'http://'.$this->input->server('HTTP_HOST');
        $host = site_url();
        $url = site_url('find_pwd/reset_pwd/'.$uniqid);

        //发送者
        $from_account = $this->get_sender_email();
        //$html = '<div style="font-size:18px;font-weight:bold;">尊敬的用户：</div><div>您正在通过外发无忧用户中心申请找回密码，要继续，请点击下面的按钮</div><div><a href="'.$url.'" style="display:inline-block;width:80px;height:30px;line-height:30px;background:#f60;text-align:center; color:#fff;text-decoration:none;border-radius:4px;" target="_blank">重置密码</a></div><div style="color:#666;">这是一封由系统自动发出的通知邮件，无需您回复。您的意见可以发送到<a href="mailto:waifa@wowostar.com" target="_blank">waifa@wowostar.com</a>！</div>';
        $html = <<<EOT
		<style type="text/css">
		*{margin:0; padding:0;}
		</style>
		<div style="width:600px; margin:0 auto; font:14px/1.5 Microsoft Yahei UI,Microsoft Yahei,tahoma,arial,宋体; background-color:#fbfbfb;">
			<div><a href="$host" target="_blank"><img src="$host/static/img/email/logo.png" alt="易控"></a></div>
			<div style="color:#5f5f5f; font-size:13px; margin:32px 37px 65px 37px; line-height:25px;">
				<p style="margin-bottom:8px;">尊敬的用户：</p>
				<p>感谢您使用易控文件安全分享平台！</p>
				<p>您正在通过易控用户中心申请找回密码，要继续，请点击下面的按钮</p>
				<p style="text-align:center; margin:35px 0;"><a href="$url" target="_blank" style="color: #ffffff; display: inline-block; font-size: 18px; height: 43px; line-height: 38px; width: 146px; background-color: #e53e46; border: 1px solid #e43e46; border-radius: 1px;  box-shadow: 0 -4px #ca181c inset; cursor: pointer; text-align: center; text-decoration:none;">重置密码</a></p>
				<p>如果以上按钮点击无效，请将下面的链接地址复制到您的浏览器地址栏（如IE）</p>
				<p>进入到易控重置密码页面。<span style="color:#817d7d;">（该链接在24小时有效，超过24小时需要重新找回密码）</span></p>
				<p><a href="$url" target="_blank" style="color:#73aeff; word-break: break-all; text-decoration:none;">$url</a></p>
			</div>
			<div style="font-size:12px; color:#8e8e8e; margin:0 37px 0 37px;">这是一封由系统自动发出的通知邮件，无需您回复。您的意见可以发送到<a href="mailto:{$from_account[0]}" target="_blank" style="color:#73aeff;">{$from_account[0]}</a></div>
			<div style="font-size:12px; color:#8e8e8e; margin:30px 37px 0 37px; text-align:center;">©2016 易控<img src="$host/static/img/email/logo-info.png" alt="我的信息我做主" style="vertical-align: middle; margin-left: 12px;"></div>
		</div>
EOT;
        $this->email->from($from_account[0], $from_account[1]);
        $this->email->to($email);
        $this->email->subject('易控-找回您的账户密码！');
        $this->email->message($html);
        //$this->email->send();

        if(!$this->email->send()){
            return $this->set_error(6001);
        }
        else
        {
            //发送成功后更新数据表
            $data = array(
                'process'   => 2,//找回密码流程
                'uid'       => $user_info['id'],//用户id
                'way'       => 1,//认证方式 邮件
                'receiver'  => $email,//接受者
                'code'    => $uniqid,//校验码
                'code_hash' => Common::time33($uniqid),//校验码的哈希值
                'time'      => time()
            );
            //查找是否已经存在记录
            $map=array(
                'process'=>2,
                'way'=>1,
                'uid'=>$user_info['id'],
                'receiver'=>$email
            );
            $find = $this->user_model->get_verify_info($map,"id,num");
            if(!empty($find)){
                //更新
                $num = (int)$find['num'];
                $data['num'] = ++$num;
                $result = $this->user_model->update_verify_info_by_id($find['id'], $data);
            } else {
                //插入新记录
                $result = $this->user_model->create_verify_info($data);
            }
            if($result===FALSE)
            {
                return $this->set_error(1002);
            }
            else
            {
                return TRUE;
            }
        }
    }

    /**
     * 检查是否可以发送找回密码邮件
     * @param $email
     * @return bool
     */
    public function can_send_find_password_email($email)
    {
        //检查email是否有关联的用户
        $user_info = $this->user_model->get_user_info_by_email($email);
        if(empty($user_info))
        {
            return $this->set_error(5004);//用户不存在
        }
        $result = $this->get_verify_info_by_email(2,$email);
        if(!empty($result)){
            //防止不断刷新发送邮件
            $time = time() - (int)$result['time'];
            $times = $this->config->item("email_times","user_common");
            //$exp = $this->config->item("email_verify_code_exp","user_common");
            $interval_time = $this->config->item("email_interval_time","user_common");
            //1分钟内只能发送一次
            if($interval_time!=0 && $time < $interval_time)
            {
                return $this->set_error(6003,array(Common::format_second($interval_time)));
            }
            //24小时内只能发送5次
            if($times !=0 && (int)$result['num'] >= $times && $time <= 86400)
            {
                return $this->set_error(6004,array($times));
            }
            if($time>86400)
            {
                $this->user_model->delete_verify_info(array("id"=>$result['id']));
                return TRUE;
            }
        }
        return TRUE;
    }




    /**
     * 是否能发送邮箱
     * @param $email
     * @param int $process
     * @return bool|mixed
     */
    public function can_send_email($email,$process=1,$is_link=FALSE)
    {
        $where=array(
            "receiver"=>$email,
            "process" =>$process,
            "way"     =>$is_link?self::WAY_EMAIL_LINK:self::WAY_EMAIL //1：邮箱链接 2:邮箱验证码
        );
        $info = $this->user_model->get_verify_info($where,"id,time,num");
        if(empty($info))
        {
            return TRUE;
        }
        else
        {
            $time = time() - (int)$info['time'];
            $times = $this->config->item("email_times","user_common");
            //$exp = $this->config->item("sms_verify_code_exp","user_common");
            $interval_time = $this->config->item("email_interval_time","user_common");
            //Common::console($interval_time."===".$time);
            //1分钟内只能发送一次
            if($interval_time!=0 && $time < $interval_time)
            {
                $info['live_time'] = (int)$info['time'] + $interval_time;
                return $this->set_error(6003,array(Common::format_second($interval_time)),$info);
            }
            //24小时内只能发送5次
            if($times !=0  && (int)$info['num'] >= $times && $time <= 86400)
            {
                $info['live_time'] = (int)$info['time']+86400;
                return $this->set_error(6004,array($times),$info);
            }
            //超过24小时的记录删除
            if($time>86400)
            {
                $this->user_model->delete_verify_info(array("id"=>$info['id']));
                return TRUE;
            }
            return $info;
        }
    }

    public function send_email($email,$process=1,$link=FALSE,$uid=0,$del_other=FALSE)
    {
        if(empty($email))
        {
            return $this->set_error(1003);
        }
        $is_link = !empty($link)?TRUE:FALSE;
        $info = $this->can_send_email($email,$process,$is_link);
        if($info===FALSE)
        {
            return FALSE;
        }
        //是否删除其他同一用户同一流程的其他接受者的验证信息
        if(!empty($uid) && $del_other)
        {
            $del = $this->user_model->delete_verify_info(array("uid"=>$uid,"process"=>$process,"way"=>1),"receiver <> '{$email}'");
            if($del===FALSE)
            {
                return $this->set_error(1002);
            }
        }

        $host = site_url();
        $feedback_account = $this->config->item("feedback_email_from_account","user_common");
        if($link)
        {
            $tpl_data = $this->lang->line("email_verify_link_code_tpl");
            if(!isset($tpl_data[$process]))
            {
                return $this->set_error(1003);
            }
            $tpl = $tpl_data[$process];
            $title = $tpl['title'];
            //生成唯一标识符
            $code = Common::uniqid();
            //以下设置Email内容$host = 'http://'.$this->input->server('HTTP_HOST');
            $url = site_url($link."/".$code);
            $content = $tpl['content'];
            $content = str_replace(array("[host]","[url]","[from]"),array($host,$url,$feedback_account[0]),$content);
        }
        else
        {
            $code_exp = $this->config->item("email_verify_code_exp","user_common");
            $code_len = $this->config->item("email_verify_code_length","user_common");
           // $tpl = $this->config->item("email_verify_code_tpl","user_common");
            $tpl_data = $this->lang->line("email_verify_code_tpl");
            $tpl = $tpl_data[$process];
            $title = $tpl['title'];
            $code = Common::random_number($code_len);
            $exp_min = Common::format_second($code_exp);
            $content = $tpl['content'];
            $content = str_replace(array("[host]","[from]","[code]","[min]"),array($host,$feedback_account[0],$code,$exp_min),$content);

        }
        $from_account = $this->get_sender_email();
        $this->email->from($from_account[0], $from_account[1]);
        $this->email->to($email);
        $this->email->subject($title);
        $this->email->message($content);
        if($this->email->send())
        {
            if($info===TRUE)
            {
                $data = array(
                    "process"=>$process,
                    "way"    =>$link?self::WAY_EMAIL_LINK:self::WAY_EMAIL,
                    "code"=>$code,
                    "code_hash"=>$link?Common::time33($code):0,
                    "receiver"=>$email,
                    "num"=>1,
                    "time"=>time(),
                    "session_id"=>$this->session->session_id,
                    "uid"=>$uid
                );
                $save = $this->user_model->create_verify_info($data);
            }
            else
            {
                $data=array(
                    "code_hash"=>$link?Common::time33($code):0,
                    "code"=>$code,
                    "time"=>time(),
                    "num"=>((int)$info['num']+1)
                );
                $save = $this->user_model->update_verify_info_by_id($info["id"],$data);
            }
            if($save===FALSE)
            {
                return $this->set_error(1002);
            }
            else
            {
                return true;
            }
        }
        else
        {
            //echo $this->email->print_debugger('');exit;
            log_message("debug",$this->email->print_debugger());
            return $this->set_error(6001);
        }
    }



    /**
     * 通过email获取校验信息
     * @param $process 校验流程
     * @param $email 邮件
     * @param string $field
     * @return mixed
     */
    public function get_verify_info_by_email($process=1,$email,$field="id,num,time"){
        $where=array(
            'process'=>$process,
            'way'   =>1,
            'receiver'=>$email
        );
        return $this->user_model->get_verify_info($where,$field);
    }

    /**
     * 通过email来验证用户
     * @param $email
     */
    public function check_user_by_email($email,$field="id,verify,status")
    {
        $where['email']=$email;
        $info = $this->user_model->get_user_info($where,$field);
        return $this->check_user($info);
    }

    public function check_user_by_phone($phone,$fields="id,verify,status")
    {
        $where['mobile']=$phone;
        $info =  $this->user_model->get_user_info($where,$fields);
        return $this->check_user($info);
    }
    private function check_user($info)
    {
        if(empty($info))
        {
            return $this->set_error(5004);
        }
        else if($info['status']==0)
        {
            return $this->set_error(5005);
        }
        else if($info['verify']==0)
        {
            return $this->set_error(5006);
        }
        else
        {
            return $info;
        }
    }


    /**
     *
     * 检查邮箱验证码
     * @param $code
     * @param string $email
     * @param int $process
     * @param bool $is_link
     * @param bool $del
     * @return mixed
     */
    public function check_email_verify_code($code,$email="",$process=1,$is_link=FALSE,$del=TRUE)
    {
        if(empty($code))
        {
            return $this->set_error(1003);
        }
        if($is_link)
        {
            //$where['code_hash'] = Common::time33($code);
            $where['code'] = $code;
            $where['process'] = $process;
            $where['way'] = self::WAY_EMAIL_LINK;//邮件
        }
        else
        {
            //receiver
            $where['receiver'] = $email;
            $where['code'] = $code;
            $where['process'] = $process;
            $where['way'] = self::WAY_EMAIL;
        }

        $find = $this->user_model->get_verify_info($where,'id,uid,num,time,receiver,session_id');
        $exp = $this->config->item('email_verify_code_exp','user_common');
        if(empty($find))//验证码不存在
        {
            return $this->set_error(6002);
        }
        else if(time()-(int)$find['time'] > $exp)//过期
        {
            return $this->set_error(6002);
        }
        else
        {
            //删除验证记录
            if($del)
            {
                $this->user_model->delete_verify_info(array("id"=>$find['id']));
            }
            return $find;
        }
    }

    /**
     * 验邮件的验证码 (该验证方式的code是一个全局唯一的md5值，如果是明文验证码请勿用该方法校验)
     * @param $code
     * @return bool
     */
    public function check_email_code($code,$del=TRUE)
    {
        if(empty($code))
        {
            return $this->set_error(1003);
        }
        $where['code_hash'] = Common::time33($code);
        $where['code'] = $code;
        $where['way'] = 1;//邮件
        $find = $this->user_model->get_verify_info($where,'id,uid,num,time,receiver,session_id');
        $exp = $this->config->item('email_verify_code_exp','user_common');
        if(empty($find))//验证码不存在
        {
            return $this->set_error(1004);
        }
        else if(time()-(int)$find['time'] > $exp)//过期
        {
            return $this->set_error(6002);
        }
        else
        {
            //校验成功标记用户为已经验证用户
            $this->user_model->update($find['uid'],array('verify' => 1));
            //删除验证记录
            if($del)
            {
                $this->user_model->delete_verify_info(array("id"=>$find['id']));
            }
            return $find;
        }
    }


    /**
     * 检查是否可以发送短信验证码
     * @param $mobile 手机号
     * @param int $process 流程 1:注册 2:找回密码
     * @return bool|mixed
     */
    public function can_send_sms($mobile,$process=1)
    {
        $where=array(
            "receiver"=>$mobile,
            "process" =>$process,
            "way"     =>2 //1：邮箱 2:短信
        );
        $info = $this->user_model->get_verify_info($where,"id,time,num");
        if(empty($info))
        {
            return TRUE;
        }
        else
        {
            $time = time() - (int)$info['time'];
            $times = $this->config->item("sms_times","user_common");
            //$exp = $this->config->item("sms_verify_code_exp","user_common");
            $interval_time = $this->config->item("sms_interval_time","user_common");
            //Common::console($interval_time."===".$time);
            //1分钟内只能发送一次
            $info['live_time'] = time()+$interval_time;
            if($interval_time!=0 && $time < $interval_time)
            {
                $info['live_time'] = (int)$info['time'] + $interval_time;
                return $this->set_error(6003,array(Common::format_second($interval_time)),$info);
            }
            //24小时内只能发送5次
            if($times !=0  && (int)$info['num'] >= $times && $time <= 86400)
            {
                $info['live_time'] = (int)$info['time']+86400;
                return $this->set_error(6004,array($times),$info);
            }
            //超过24小时的记录删除
            if($time>86400)
            {
                $this->user_model->delete_verify_info(array("id"=>$info['id']));
                return TRUE;
            }
            return $info;
        }
    }

    /**
     * 发送短信验证码
     * @param $mobile 手机号
     * @param $process 流程 1:注册 2:找回密码
     * @return bool|mixed
     */
    public function send_sms($mobile,$process=1,$uid=0,$del_other=FALSE)
    {
        if(empty($mobile))
        {
            return $this->set_error(1003);
        }
        $info = $this->can_send_sms($mobile,$process);
        if($info===FALSE)
        {
            return FALSE;
        }
        //是否删除其他同一用户同一流程的其他接受者的验证信息
        if(!empty($uid) && $del_other)
        {
            $del = $this->user_model->delete_verify_info(array("uid"=>$uid,"process"=>$process,"way"=>2),"receiver <> '{$mobile}'");
            if($del===FALSE)
            {
                return $this->set_error(1002);
            }
        }

        $code_exp = $this->config->item("sms_verify_code_exp","user_common");
        $code_len = $this->config->item("sms_verify_code_length","user_common");
        $tpl_data = $this->lang->line("sms_verify_code_tpl");
        $tpl = $tpl_data[$process];
        $code = Common::random_number($code_len);
        $exp_min = Common::format_second($code_exp);
        $content = sprintf($tpl,$code,$exp_min);
        $send = $this->sms->send($mobile,$content);
        if($send)
        {
            if($info===TRUE)
            {
                $data = array(
                    "process"=>$process,
                    "way"=>2,
                    "code"=>$code,
                    "code_hash"=>Common::time33($code),
                    "receiver"=>$mobile,
                    "num"=>1,
                    "time"=>time(),
                    "session_id"=>$this->session->session_id,
                    "uid"   => $uid
                );
                $save = $this->user_model->create_verify_info($data);
            }
            else
            {
                $data=array(
                    "code"=>$code,
                    "time"=>time(),
                    "num"=>((int)$info['num']+1)
                );
                $save = $this->user_model->update_verify_info_by_id($info["id"],$data);
            }
            if($save===FALSE)
            {
                return $this->set_error(1002);
            }
            else
            {
                return TRUE;
            }
        }
        else
        {
            return $this->set_error(6005);
        }
    }

    /**
     * 检查短信验证码是否正确
     * @param $code
     * @param $mobile
     * @param int $process
     * @param bool $del
     * @return mixed
     */
    public function check_sms_code($code,$mobile,$process=1,$del=TRUE)
    {
        if($code=='' || empty($mobile) || empty($process))
        {
            return $this->set_error(1003);
        }
        $where['receiver']=$mobile;
        $where['code'] = $code;
        $where['way'] = 2;//短信
        $where['process']=$process;//流程

        $find = $this->user_model->get_verify_info($where,'id,num,time,session_id');
        $exp = $this->config->item('sms_verify_code_exp','user_common');
        if(empty($find))//验证码不存在
        {
            return $this->set_error(6007);
        }
        else if(time()-(int)$find['time'] > $exp)//过期
        {
            return $this->set_error(6002);
        }
        else
        {
            //删除验证记录
            if($del)
            {
                $this->user_model->delete_verify_info(array("id"=>$find['id']));
            }
            return $find;
        }
    }


    public function delete_verify_info($where)
    {
        return $this->user_model->delete_verify_info($where);
    }

    /**
     * 续期验证码
     * @param $where
     * @return mixed
     */
    public function renew_verify_code($where) {
        return $this->user_model->renew_verify_code($where);
    }

}